package com.tiger.web.controller.system;

import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxMapResult;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.system.domain.SysOperation;
import com.tiger.system.service.ISysOperationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 运营管理Controller
 *
 * @author standard
 * @date 2022-10-20
 */
@Api(tags = "运营管理")
@RestController
@RequestMapping("/system/operation")
public class SysOperationController extends BaseController {
    @Autowired
    private ISysOperationService sysOperationService;

    /**
     * 查询运营管理列表
     */
    @ApiOperation(value = "查询运营管理列表")
    //@PreAuthorize("@ss.hasPermi('system:operation:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOperation sysOperation) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        sysOperation.setCompanyId(loginUser.getDeptId());
        startPage();
        List<SysOperation> list = sysOperationService.selectSysOperationList(sysOperation);
        return getDataTable(list);
    }



    /**
     * 获取运营管理详细信息
     */
    @ApiOperation(value = "获取运营管理列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:operation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysOperationService.selectSysOperationById(id));
    }

    /**
     * 新增运营管理
     */
    @ApiOperation(value = "新增运营管理")
    //@PreAuthorize("@ss.hasPermi('system:operation:add')")
//    @Log(title = "运营管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysOperation sysOperation) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        sysOperation.setCreatedAt(DateUtils.getNowDate());
        sysOperation.setCompanyId(loginUser.getDeptId());
        sysOperation.setCreateById(loginUser.getUserId());
        sysOperation.setCreateByName(loginUser.getUser().getNickName());
        return toAjax(sysOperationService.insertSysOperation(sysOperation));
    }

    /**
     * 修改运营管理
     */
    @ApiOperation(value = "修改运营管理")
    //@PreAuthorize("@ss.hasPermi('system:operation:edit')")
//    @Log(title = "运营管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysOperation sysOperation) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        sysOperation.setUpdateAt(DateUtils.getNowDate());
        sysOperation.setUpdateById(loginUser.getUserId());
        sysOperation.setUpdateByName(loginUser.getUser().getNickName());
        return toAjax(sysOperationService.updateSysOperation(sysOperation));
    }

    /**
     * 删除运营管理
     */
    @ApiOperation(value = "删除运营管理")
    //@PreAuthorize("@ss.hasPermi('system:operation:remove')")
//    @Log(title = "运营管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(sysOperationService.deleteSysOperationById(id));
    }

    /**
     * 文章统计数量
     */
    @ApiOperation(value = "文章数量")
    @GetMapping(value = "/count")
    public AjaxMapResult count(){
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return AjaxMapResult.success(sysOperationService.count(loginUser));
    }

    /**
     * 文章上下线
     */
    @ApiOperation(value = "文章上下线")
    @GetMapping("/longer")
    public AjaxResult longer(Long id,@ApiParam(value = "1-上线  2-下线") Long state){
        return toAjax(sysOperationService.longer(id,state));
    }

}
